#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll func(ll& ret)
{
	string s=to_string(ret);
	ll n=s.size();
	ll k=0;
	for(ll i=0;i<n;++i)
	{
		k+=(s[i]-'0')*(s[i]-'0');
	}
	return k;
}

int main()
{
	ll mymax=0; 
	ll i=10000;
	ll x=0;
	cin>>x;
	while(i--)
	{
		x=func(x);
		mymax=max(mymax,x);
	 } 
	cout<<mymax;
  return 0;
}
